package studentinformationmanagementsystem.dao.impl;

import studentinformationmanagementsystem.dao.BaseDao;
import studentinformationmanagementsystem.dao.StudentDao;
import studentinformationmanagementsystem.entity.Student;

import java.util.List;

/**
 * @author 苗庆
 * @version 1.0
 */
public class StudentDaoImpl extends BaseDao implements StudentDao {


    /**
     *添加学生信息
     * @param student  学生对象
     * @return 返回值为-1表示失败
     */
    @Override
    public int insert(Student student) {
        String sql = "INSERT INTO `t_student` (`studentid`, `studentname`, `idnumber`, `sex`, `birthdate`, `nation`, `politicsstatus`," +
                " `photograph`, `phone`, `mailbox`,`classandgrade`,`major`,`department`,`homeaddress`,`emergencycontact`,`emergencycontactphone`," +
                "`nativeplace`,`password`,`AdmissionDate`) VALUES (?, ?, ?, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        return super.update(sql, student.getStudentid(), student.getStudentname(), student.getIdnumber(), student.getSex(), student.getBirthdate(),
                student.getNation(), student.getPoliticsstatus(), student.getPhotograph(), student.getPhone(), student.getMailbox(), student.getClassandgrade(),
                student.getMajor(),student.getDepartment(),student.getHomeaddress(),student.getEmergencycontact(),student.getEmergencycontactphone(),
                student.getNativeplace(),student.getPassword(),student.getAdmissionDate());
    }

    /**
     * 根据学生ID进行学生信息更新
     * @param student  学生对象
     * @return 返回值为-1表示失败
     */
    @Override
    public int update(Student student) {
        String sql = "UPDATE `t_student` SET `studentid` = ? , `studentname` = ? , `idnumber` = ? , `sex` = ? , `birthdate` = ? , " +
                "`nation` = ? ,`politicsstatus` = ? , `photograph` = ? , `phone` = ? , `mailbox` = ?  ,`classandgrade` = ? ,`major` = ?," +
                "`department` = ? ,`homeaddress` = ? ,`emergencycontact` = ? ,`emergencycontactphone` = ? ,`nativeplace` = ? ,`password` = ? ,`AdmissionDate` = ? WHERE `studid` = ? ";
        return super.update(sql, student.getStudentid(), student.getStudentname(), student.getIdnumber(), student.getSex(), student.getBirthdate(),
                student.getNation(), student.getPoliticsstatus(), student.getPhotograph(), student.getPhone(), student.getMailbox(), student.getClassandgrade(),
                student.getMajor(),student.getDepartment(),student.getHomeaddress(),student.getEmergencycontact(),student.getEmergencycontactphone(),
                student.getNativeplace(),student.getPassword(),student.getAdmissionDate(),student.getStudid());
    }

    /**
     * 根据学生ID进行学生信息删除
     * @param student 学生ID
     * @return 返回值为-1表示失败
     */
    @Override
    public int delete(Student student) {
        String sql = "DELETE FROM `t_student` WHERE `studid` = ?";
        return super.update(sql, student.getStudid());
    }

    /**
     * 查询所有的学生信息 lisi.size==0 失败  大于0成功
     * @return
     */
    @Override
    public List<Student> queryAll() {
        String sql = "SELECT * FROM `t_student`";
        return super.queryForList(Student.class, sql);
    }

    /**
     * 根据学生ID查询学生信息
     * @param student 学生ID
     * @return 学生信息
     */
    @Override
    public Student queryById(Student student) {
        String sql = "SELECT * FROM `t_student` WHERE `studid` = ?";
        return super.queryForOne(Student.class, sql, student.getStudid());
    }

    /**
     * 根据学号即账号查询用户信息
     * @param student 学号名
     * @return 学生信息
     */
    @Override
    public Student queryByUsername(Student student) {
        String sql = "SELECT * FROM `t_student` WHERE `studentid` = ?";
        return super.queryForOne(Student.class, sql, student.getStudentid());
    }

    /**
     * 根据学号即账号和密码查询用户信息
     * @param student 带有学号和密码的学生信息
     * @return 学生信息
     */
    @Override
    public Student queryByUsernameAndPassword(Student student) {
        String sql = "SELECT * FROM `t_student` WHERE `studentid` = ? AND `password` = ?";
        return super.queryForOne(Student.class, sql, student.getStudentid(), student.getPassword());
    }


    /**
     * 查询总记录条数
     */
    @Override
    public Integer queryForPageTotalCount() {
        String sql = "SELECT COUNT(1) FROM `t_student`";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }


    /**
     * 分页查询DAO层方法
     * @param begin    起始记录行数
     * @param pageSize 每页展示数量
     * @return
     */
    @Override
    public List<Student> queryForPageItems(int begin, int pageSize) {
        String sql = "SELECT * FROM `t_student` LIMIT ?, ?";
        return super.queryForList(Student.class, sql, begin, pageSize);
    }

    /**
     * 根据学生学号、身份证号、手机号查询学生信息
     * @param student 带有学号、身份证号、联系电话的学生信息
     * @return
     */
    @Override
    public Student queryByStudentidAndIdnumberAndPhone(Student student) {
        String sql = "SELECT * FROM `t_student` WHERE `studentid` = ? AND `idnumber` = ? AND `phone` = ?";
        return super.queryForOne(Student.class, sql, student.getStudentid(), student.getIdnumber(),student.getPhone());
    }
}
